package com.example.testnetwork;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.widget.RemoteViews;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

/**
 * 这个service在onStartCommand里启动了一个定时器，这个定时器用于每隔4分钟检测一次网络状态
 */

public class KeepLiveService extends Service {
    String TAG = "KeepLiveService";
    NotificationManager mNotificationManager;

    @Override
    public void onCreate() {
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        super.onCreate();
    }


    /**
     *  在这里显示通知栏、启动定时任务去检查网络状态
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //显示通知栏：提示正在检测网络状态
        Notification notification = getNotifycation();
        showNotify(notification);

        //启动一个定时器，每隔4分钟检测一次网络状态
        startAlarmForCheckNetwork();
        return START_STICKY;
    }

    /**
     * 启动定时器
     */
    public void startAlarmForCheckNetwork() {
        AlarmManager alarmManagerUtils = AlarmManager.getInstance(getApplicationContext());
        alarmManagerUtils.createAlarmManager();
        alarmManagerUtils.startAlarmManager();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /*
     * 创建 Notification
     * @return
     */
    public Notification getNotifycation() {
        if (Build.VERSION.SDK_INT >= 26) {
            return getNotificationForAndroid_O();
        } else {
            return getNotification();
        }
    }

    /*
     * 创建 Notification for Android O及以上
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private Notification getNotificationForAndroid_O() {
        String channelId = getApplicationContext().getResources().getString(R.string.notify_channel_Id);
        String channelName = getApplicationContext().getResources().getString(R.string.notify_channel_name);
        //创建通知渠道
        createNotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);

        RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom);
        view_custom.setImageViewResource(R.id.custom_icon, R.mipmap.ic_launcher);
        view_custom.setTextViewText(R.id.title_notify, "正在用NetWorkInfo检测网络状态");
        String appName = this.getResources().getString(R.string.app_name) + "APP";

        Notification notification = new Notification.Builder(this, channelId)
                .setContentTitle(appName)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setOngoing(true)
                .setOnlyAlertOnce(true)
                .setSound(null)
                .setVibrate(null)
                .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)//取消震动,铃声其他都不好使
                .build();
        notification.defaults = 0;
        notification.sound = null;
        notification.vibrate = null;
        notification.contentView = view_custom;

        return notification;
    }
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        channel.setSound(null, null);
        notificationManager.createNotificationChannel(channel);
    }
    /*
     * 创建 Notification for Android O以下操作系统
     */
    private Notification getNotification() {

        RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom);
        String title_notify = getApplication().getResources().getString(R.string.notify_content);
        view_custom.setImageViewResource(R.id.custom_icon, R.mipmap.ic_launcher);
        view_custom.setTextViewText(R.id.title_notify, title_notify);

        NotificationCompat.Builder mBuilder;
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setVibrate(null)
                .setSound(null)
                .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE)
                .setOnlyAlertOnce(true)
                .setOngoing(true);
        Notification notification = mBuilder.build();
        notification.sound = null;
        notification.defaults = 0;
        notification.vibrate = null;
        notification.contentView = view_custom;
        return notification;
    }

    /**
     * 显示通知栏
     */
    public void showNotify(Notification notification) {
        mNotificationManager.notify(100, notification);
        startForeground(100, notification);
        return;
    }
}